package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.StatusInfo;
import com.lkd.contract.VmStatusContract;
import com.lkd.emq.Topic;
import com.lkd.feign.VMService;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskService;
import com.lkd.utils.JsonUtil;
import com.lkd.vo.VmVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
@Topic(TopicConfig.VMS_STATUS_TOPIC)
public class AutoCheckVmStatusHandler implements MsgHandler {

    @Autowired
    private TaskService taskService;

    @Autowired
    private VMService vmService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void process(String jsonMsg) throws IOException {
        //接收设备信息
        VmStatusContract contract = JsonUtil.getByJson(jsonMsg, VmStatusContract.class);
        String innerCode = contract.getInnerCode();
        //校验设备状态是否有故障
        List<StatusInfo> statusInfos = contract.getStatusInfo();
        for (StatusInfo statusInfo : statusInfos) {
            //出现故障,自动创建维修工单
            if (!statusInfo.isStatus()) {

                //查询故障设备信息
                VmVO vmInfo = vmService.getVMInfo(innerCode);
                //设置工单信息
                TaskViewModel model = new TaskViewModel();
                model.setCreateType(0);
                model.setInnerCode(innerCode);
                //获取当日工单最少的该区域的运维人员id
                Integer userid = taskService.getRepairId(vmInfo);
                model.setUserId(userid);
                model.setProductType(VMSystem.TASK_TYPE_REPAIR);
                model.setDesc("维修工单");
                //创建工单
                taskService.createTask(model);
                return;
            }
        }

    }
}
